import { _decorator, Node, tween, Tween, v3 } from "cc";
import { $planet_BaseUI_poof$ } from "./ui.base";

const {ccclass, property} = _decorator;

@ccclass
export abstract class $planet_BasePop_poof$<$planet_POP_poof$, $planet_RETURN_poof$> extends $planet_BaseUI_poof$ {
    /** 弹窗是否被关闭 */
    public get $planet_is_close_poof$(){
        return this.$planet_npromise_poof$==undefined;
    }
	private $planet_npromise_poof$: abd.$planet_NudityPromise_poof$<$planet_RETURN_poof$>;
    private $planet_closed_poof$: boolean;
    /** 生成一个promise实例 */
    protected $planet_promise_poof$(){
        this.$planet_closed_poof$ = false;
		this.$planet_npromise_poof$ = new abd.$planet_NudityPromise_poof$();
        return this.$planet_npromise_poof$.$planet_promise_poof$;
    }
    /**
     * 关闭弹窗
     * @param param 回复数据，如果不传，则会被当作弹窗操作失败传入默认参数
     */
    protected $planet_close_poof$(param?: $planet_RETURN_poof$){
        if(this.$planet_closed_poof$) return undefined;
        else this.$planet_closed_poof$ = true;
        // 调用回调函数
        this.$planet_onClose_poof$(param);
        // 回复promise
        if(param!==undefined) this.$planet_npromise_poof$.$planet_resolve_poof$(param);
        else this.$planet_npromise_poof$.$planet_reject_poof$(this.$planet_default_return_poof$);
        // 清空promise相关的响应
		this.$planet_npromise_poof$ = undefined;
    }

    /** 从外部关闭弹窗 */
    public $planet_closeFromOutside_poof$(){
        this.$planet_close_poof$(this.$planet_default_return_poof$);
    }

    /** 默认返回值 */
    protected abstract $planet_default_return_poof$: $planet_RETURN_poof$;
	/** 管理节点 */
	protected $planet_pop_poof$: $planet_POP_poof$;
	/**
	 * 显示弹窗并返回promise
	 * @param args
	 */
	public $planet_show_poof$(pop: $planet_POP_poof$, ...args: Parameters<typeof this.$planet_onShow_poof$>){
		this.$planet_pop_poof$ = pop;
		this.node.active = true;
		this.$planet_call_func_poof$(this.$planet_onShow_poof$, ...args);
		return this.$planet_promise_poof$();
	}
    /**
     * 显示时调用
     * @param param 可选参数
     */
    public abstract $planet_onShow_poof$(...param: any[]): void;
    /** 关闭弹窗时的回调 */
    protected $planet_onClose_poof$(param: $planet_RETURN_poof$): void{};

	/**
	 * 果冻效果（入场）
	 * @param node
	 */
	protected static $planet_jelly_enter_poof$(node: Node, duration?: number){
		let np = new abd.$planet_NudityPromise_poof$<Node>();
		Tween.stopAllByTarget(node);
		duration = duration ?? 0.5;

		tween(node)
		.set({scale: v3(0.8, 0.8, 1)})
		.to(duration, {scale: v3(1, 1, 1)}, {easing: 'elasticOut'})
		.set({scale: v3(1, 1, 1)})
		.call(()=>np.$planet_resolve_poof$(node))
		.start();

		return np.$planet_promise_poof$;
	}

	/**
	 * 果冻效果（离场）
	 * @param node
	 * @param duration
	 */
	protected static $planet_jelly_leave_poof$(node: Node, duration?: number){
		let np = new abd.$planet_NudityPromise_poof$<Node>();
		Tween.stopAllByTarget(node);
		duration = duration ?? 0.25;

		tween(node)
		.to(duration, {scale: v3(0.8, 0.8, 1)}, {easing: "backIn"})
		.set({scale: v3(0.8, 0.8, 1)})
		.call(()=>np.$planet_resolve_poof$(node))
		.start();

		return np.$planet_promise_poof$;
	}
}
